package com.jerei.crm.controller;

import com.jerei.crm.entity.User;
import com.jerei.crm.service.IUserService;
import com.jerei.crm.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController{


    @Autowired
    private IUserService userService;

    @RequestMapping("/add")
    public JsonResult<Void> addUser(String username,String password){

        System.out.println("控制层");
        userService.addUser(username,password);
        return new JsonResult<>(OK);

    }

    @RequestMapping("/login")
    public JsonResult<User> login(Integer checked, String username, String password, HttpSession session){
        User data=userService.login(username,password);
        if(checked==1){
            data.setPassword(password);
        }
        session.setAttribute("uid",data.getUid());
        session.setAttribute("username",data.getUsername());
        return new JsonResult<>(OK,data);
    }

@RequestMapping("/logout")
    public JsonResult<Void> logout(HttpSession session){
        session.removeAttribute("uid");
        session.removeAttribute("username");
        return new JsonResult<>(OK);
    }








}
